<?php
	require_once 'header.php';
	require_once 'navigation.php';
	$smarty->display('user_left.html');
	
	//Get Current ColumnID
	if (isset($_SESSION['unc']) && $_SESSION['unc'] != '') {
		$smarty->assign('isLogin', 'T');
		$usernc = $_SESSION['unc'];
		
		//Get State List
		$stateArray = array();
		$stateArray[0]="已保存";
		$stateArray[1]="已提交";
		$stateArray[2]="已确认";
		$stateArray[3]="合同初稿";
		$stateArray[4]="客服联系";
		$stateArray[5]="书面合同/未付款";
		$stateArray[6]="付款/未发货";
		$stateArray[7]="发货";
		$stateArray[8]="完成";
		$stateArray[9]="已过期";
		
		$ordersql = "select o.id,o.fdSyncID,o.fdCreate,o.fdSubmit,o.fdStatus from ".$util->contentdbName().".tbOrder o left outer join ".$util->userdbName().".tbUser u on o.fdUserID = u.id  where u.fdLogin = '".$usernc."'";		
		$orders = $adminDB->executeSQL($ordersql, $connID);			
		$arrayOrderInfos = array();		
		for($i=0;$i<count($orders);$i++){
			$tmpOrder = array();
			$arrayItemInfos = array();
			$totalPrice = 0;
			$tmpOrder['id'] = $orders[$i]['id'];
			$tmpOrder['sync'] = $orders[$i]['fdSyncID'];
			$tmpOrder['create'] = $orders[$i]['fdCreate'];
			$tmpOrder['submit'] = $orders[$i]['fdSubmit'];
			$tmpOrder['status'] = $orders[$i]['fdStatus'];
			$tmpOrder['state'] = $stateArray[$orders[$i]['fdStatus']];
			$orderid=$orders[$i]['id'];
			
			if ($orderid != '') {
				$tmpArray = array();
				$itemsql = "select i.id, i.fdOrderID,i.fdProductID,i.fdDate,i.fdPrice,i.fdCount,i.fdStatus,p.fdName from ".$util->contentdbName().".tbItem i left outer join ".$util->contentdbName().".tbProduct p on p.id = i.fdProductID where i.fdOrderID = ".$orderid."";
				$items = $adminDB->executeSQL($itemsql, $connID);
				
				for($j=0;$j<count($items);$j++){
					$tmpArray['id'] = $items[$j]['id'];
					$tmpArray['name'] = $items[$j]['fdName'];
					$tmpArray['orderid'] = $items[$j]['fdOrderID'];
					$tmpArray['productid'] = $items[$j]['fdProductID'];
					$tmpArray['price'] = $items[$j]['fdPrice'];
					$tmpArray['num'] = $items[$j]['fdCount'];
					$tmpArray['smallTotalPrice'] = $items[$j]['fdPrice'] * $items[$j]['fdCount'];
					$totalPrice += $tmpArray['smallTotalPrice'];
					array_push($arrayItemInfos, $tmpArray);
				}				
			}
			$tmpOrder['items']=$arrayItemInfos;
			$tmpOrder['totalprice']=$totalPrice;
			array_push($arrayOrderInfos, $tmpOrder);
		}
		$smarty->assign('arrayOrderInfos', $arrayOrderInfos);
	} else {
		$smarty->assign('isLogin', 'F');
		if (isset($_SESSION['toUrl'])) {
			unset($_SESSION['toUrl']);
		}
		$_SESSION['toUrl'] = 'userorder.php';
		
		echo "<script>alert('请先登录！');window.location.href='login.php';</script>";	
	}
	
	
	$smarty->display('user_order.html');
	require_once 'contact.php';
	require_once 'footer.php';
?>